package carcassonne.game.player;

import carcassonne.user.UserInfo;
import carcassonne.user.UserManager;
import carcassonne.utils.StringUtils;
import io.netty.channel.Channel;
import io.netty.channel.ChannelId;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PlayerManager {

    private static final PlayerManager Instance = new PlayerManager();

    public static PlayerManager GetInstance() {
        return Instance;
    }

    private final Map<String, String> AllPlayer = new ConcurrentHashMap<>();

    private final Map<String, Channel> PlayerChannelMapping = new ConcurrentHashMap<>();
    private final Map<ChannelId, String> ChannelPlayerMapping = new ConcurrentHashMap<>();

//    public UserInfo GetPlayer(Channel channel) {
//        String token = ChannelUserMapping.get(channel.id());
//        if (StringUtils.IsNullOrEmpty(token)) return null;
//        return GetUser(token);
//    }

    public boolean IsPlayerConnected(String token) {
        return PlayerChannelMapping.containsKey(token);
    }

    public void AddConnection(String playerID, Channel channel) {
        PlayerChannelMapping.put(playerID, channel);
        ChannelPlayerMapping.put(channel.id(), playerID);
    }

    public void CloseConnection(Channel channel) {
        if (!ChannelPlayerMapping.containsKey(channel.id())) return;
        String playerID = ChannelPlayerMapping.get(channel.id());
        ChannelPlayerMapping.remove(channel.id());
        PlayerChannelMapping.remove(playerID);
    }

    public Channel GetChannel(String playerID) {
        return PlayerChannelMapping.get(playerID);
    }
}
